package com.dongdongshop.seckillmq.serviceImpl;

import com.aliyuncs.exceptions.ClientException;
import com.dongdongshop_util.SendSmsDemo;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@RocketMQMessageListener(topic = "phone",consumerGroup = "dongdongSend")
public class SendSmsMQ implements RocketMQListener<String> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(String phone) {
        try {
            String code = SendSmsDemo.sendSms(phone);
            System.out.println("消费者接收验证码"+code);
            //手机为key，验证码为value
            redisTemplate.boundHashOps("User").put(phone,code);
            redisTemplate.boundHashOps("User").expire(10, TimeUnit.MINUTES);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
